package org.example.dormsystem.controller;

import org.example.dormsystem.pojo.Dormitory;
import org.example.dormsystem.pojo.Result;
import org.example.dormsystem.pojo.Student;
import org.example.dormsystem.service.StudentBoardService;
import org.example.dormsystem.service.StudentService;
import org.example.dormsystem.util.JwtUtil;
import org.example.dormsystem.util.Md5Util;
import org.example.dormsystem.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/student")
public class StudentController {

    @Autowired
    StudentService studentService;

    @Autowired
    StudentBoardService studentBoardService;

    @GetMapping("/register")
    Result register(Integer studentId,String studentName){
        Student student = studentService.findStudentByStudentId(studentId);
        if(student==null){
            if(studentService.register(studentId,studentName))
                return Result.success();
            else{
                return Result.error("未知原因注册失败");
            }
        }
        else{
            return Result.error("该学生Id已存在");
        }
    }

    @GetMapping("/login")
    Result login(Integer studentId,String studentName){
        Student student = studentService.findStudentByStudentId(studentId);
        if(student!=null){
            if(student.getStudentName().equals(studentName)){//判断学号是否正确
                Map<String,Object> claims = new HashMap<>();
                claims.put("id",student.getId());
                claims.put("studentName",student.getStudentName());
                claims.put("dormitoryNumber",student.getDormitoryNumber());
                claims.put("bedNumber",student.getBedNumber());
                claims.put("telNumber",student.getTelNumber());
                claims.put("adminNumber",student.getAdminNumber());
                String token = JwtUtil.genToken(claims);//将用户信息封装进token（生成JWT令牌）
                return Result.success(token);
            }
            else{
                return Result.error("姓名错误！");
            }
        }
        else{
            return Result.error("学号输入错误或学生不存在");
        }
    }
    @GetMapping("/studentInfo")//展示学生页面的基本信息
    Result studentInfo(){
        Map<String,Object> map = ThreadLocalUtil.get();
        Student student = studentService.findStudentByStudentId((Integer) map.get("id"));
        return Result.success(student);
    }

    @GetMapping("/studentAll")
    Result studentAll(){
        List<Student> list = studentService.getAll();
        return Result.success(list);
    }

    @GetMapping("/studentDorm")//展示学生的宿舍信息
    Result studentDorm(){
        Map<String,Object> map = ThreadLocalUtil.get();
        Dormitory dormitory = studentService.findDormIdByStudentId((String) map.get("dormitoryNumber"));
        String dormitoryNumber = dormitory.getRoomNumber();
        List<Student> list = studentService.findStudentByDormitoryNumber(dormitoryNumber);
        Map<String,Object> ans = new HashMap<>();
        ans.put("dormitory",dormitory);
        ans.put("studentList",list);
        return Result.success(ans);
    }

    @GetMapping("/studentSubmitStudent")//学生上传学生本人信息到审核表
    Result studentSubmitStudent(Student student){
        studentService.submitStudent(student);
        return  Result.success();
    }

    @GetMapping("/studentSubmit")//留言提交
    Result studentSubmit(String message){
        studentBoardService.submit(message);
        return Result.success();
    }

    @GetMapping("/studentReply")//提交回复
    Result studentReply(String message,Integer studentBoardId){
        studentBoardService.reply(message,studentBoardId);
        return Result.success();
    }

}
